package com.fengjue.filter;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Enumeration;

/**
 * 权限控制
 * 判断用户角色
 */
@Component
public class CustomUrlDecisionManager implements AccessDecisionManager {


    @Override
    public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {

        for (ConfigAttribute configAttribute : configAttributes) {
            // 当前url所需角色
            System.out.println("configAttribute = " + configAttribute);
            String needRole = configAttribute.getAttribute();
            System.out.println("needRole = " + needRole);
            // 判断角色是否登录即可访问的角色，此角色在CustomeFilter里设置
            if("ROLE_LOGIN".equals(needRole)){
                if (authentication instanceof AnonymousAuthenticationToken){
                    throw new AccessDeniedException("尚未登录，请登录");
                }else {
                    return;
                }
            }
            //判断用户角色是否为url所需角色
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
//            for (GrantedAuthority authority : authorities) {
//                System.out.println("authority = " + authority);
//                boolean equals = authority.getAuthority().equals(needRole);
//                System.out.println("equals = " + equals);
//                if (equals){
//                    return;
//                }else{
//                    throw new AccessDeniedException("权限不足,请联系管理员authority");
//                }            }
            System.out.println("authorities = " + authorities);
//            boolean contains1 = CollectionUtils.contains((Enumeration<?>) authorities, needRole);
//            boolean contains = authorities.contains(needRole);
//            System.out.println("contains1 = " + contains1);
//            System.out.println("contains = " + contains);

            for (GrantedAuthority authority : authorities) {
                String authority1 = authority.getAuthority();
                boolean equals = authority1.equals(needRole);
                if (equals){
                    return;
                }
//                else {
//                throw new AccessDeniedException("权限不足,请联系管理员authority");
//            }
            }
//            if (true){
//                return;
//            }else {
//                throw new AccessDeniedException("权限不足,请联系管理员authority");
//            }
        }
//        throw new AccessDeniedException("权限不足,请联系管理员");
    }

    @Override
    public boolean supports(ConfigAttribute attribute) {
        return false;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return false;
    }
}
